package com.alili.springbooteditor.dao;

import com.alili.springbooteditor.entities.comments;
import com.alili.springbooteditor.entities.editors;
import com.alili.springbooteditor.repository.CommentsRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public class CommentsDao {
    @Autowired
    CommentsRepository commentsRepository;
    public void save(comments comment){

        commentsRepository.save(comment);
    }
    public Page<comments> getCommentsPage(Integer editorId,Integer pageNum){
        comments comment=new comments();
        comment.setEditorId(editorId);
        ExampleMatcher matcher=ExampleMatcher.matching().withMatcher("editorId",ExampleMatcher.GenericPropertyMatchers.contains());
        Example<comments> example=Example.of(comment,matcher);//根据文章的Id查找评论
        PageRequest pageable = PageRequest.of(pageNum, 3, Sort.Direction.DESC, "visitorDate");
        return commentsRepository.findAll(example,pageable);
    }
    public void deleteComments(Integer id){
        commentsRepository.deleteById(id);
    }
    public void partDeleteCommentsByeditorId(Integer id){
        commentsRepository.deleteAllByEditorId(id);
    }
}
